/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.JobDAO;
import com.core.database.Manager;
import com.core.entities.Job;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class JobDAOImpl implements JobDAO {
Job job = new Job();
ArrayList<Job> jobsList = new ArrayList<Job>();

	Connection connection = null;
	

	private static JobDAOImpl instance=null;
	public static JobDAOImpl getInstance(){
		if(instance==null){
			instance=new JobDAOImpl();
		}
		return instance;
	}

	@Override
	public Job findByName(String name) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM job WHERE name = '" + name + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					job.setId(rs.getInt(1));
					job.setName(rs.getString(2));
					job.setDescription(rs.getString(3));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return job;
	}

	@Override
	public Job findByDescription(String description) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM job WHERE description = '"
					+ description + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					job.setId(rs.getInt(1));
					job.setName(rs.getString(2));
					job.setDescription(rs.getString(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return job;
	}

	@Override
	public void insert(Job o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO job (name, description) VALUE ( ?, ? )";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());
			ps.setString(2, o.getDescription());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Job", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Job findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM job WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					job.setId(rs.getInt(1));
					job.setName(rs.getString(2));
					job.setDescription(rs.getString(3));

					System.out.println(job);
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return job;
	}

	@Override
	public List<Job> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM job ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				job.setId(rs.getInt(1));
				job.setName(rs.getString(2));
				job.setDescription(rs.getString(3));

				jobsList.add(job);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return jobsList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM job WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, job.getName());
				ps.setString(2, job.getDescription());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Job o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE city SET name = '" + o.getName()
					+ " ', description = '" + o.getDescription()
					+ "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
				ps.setString(2, o.getDescription());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
